<!-- 部门管理 部门列表组件 -->
<template>
  <div class="">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <!-- <el-breadcrumb-item><a>用户管理</a></el-breadcrumb-item> -->
      <el-breadcrumb-item>数据权限表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-form :model="queryInfo" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="权限字段" prop="columnName">
          <el-input v-model="queryInfo.columnName" placeholder="请输入权限字段" clearable />
        </el-form-item>
        <el-form-item label="表名" prop="tableName">
          <el-input v-model="queryInfo.tableName" placeholder="请输入表名" clearable />
        </el-form-item>
   
        <el-form-item>
          <el-button @click="handleQuery" type="primary" icon="el-icon-search" size="mini">搜索</el-button>
          <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 按钮区域 -->
      <el-row :gutter="10" class="mb8" style="marginBottom:20px">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addTypeHandle">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini">删除</el-button>
        </el-col> -->
        <!-- <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini">导出</el-button>
        </el-col> -->
        <el-col :span="1.5">
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-close" size="mini" @click="closeViewHandler">返回</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="userList" style="width: 100%;margin-bottom: 20px;" row-key="id" default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column prop="scopeId" label="编号">
        </el-table-column>
        <el-table-column prop="tableName" label="表名">
        </el-table-column> 
        <el-table-column prop="columnName" label="权限字段">
        </el-table-column>
        <el-table-column prop="remark" label="描述">
        </el-table-column>
        <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="updateData(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteData(scope.row.scopeId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加字典数据 弹窗 -->
    <el-dialog :title="upload.title" width="460px" :visible.sync="dialogVisible" @close="addDialogClosed">
      <!-- 内容主题区域 -->
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="表名" prop="tableName">
              <!-- <el-input v-model="addForm.tableName"></el-input> -->
              <el-select v-model="addForm.tableName" placeholder="请选择表名" @change="handleChange">
                <el-option v-for="item in listOptions" :key="item.value" :value="item.tableName">
                  <span v-if="item.tableComment !== ''">
                    {{ item.tableComment }}--
                  </span>
                  <span v-else="item.tableComment == ''">
                    {{ item.tableComment }}
                  </span>
                  <span>{{ item.tableName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="权限字段" prop="columnName">
              <!-- <el-input v-model="addForm.columnName" ></el-input> -->
              <el-select v-model="addForm.columnName" placeholder="请选择权限字段">
                <el-option v-for="item in dataOptions" :key="item.columnName" :value="item.columnName">
                  <span v-if="item.columnComment !== ''">
                    {{ item.columnComment }}--
                  </span>
                  <span v-else="item.columnComment == ''">
                    {{ item.columnComment }}
                  </span>
                  <span>{{ item.columnName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="remark">
              <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入内容"
                v-model="addForm.remark">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: '',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      showSearch: true,
      listOptions: [],
      dataOptions:[],
      // 数据标签回显样式
      listClassOptions: [
        {
          value: 'default',
          label: '默认'
        },
        {
          value: 'primary',
          label: '主要'
        },
        {
          value: 'success',
          label: '成功'
        },
        {
          value: 'info',
          label: '信息'
        },
        {
          value: 'warning',
          label: '警告'
        },
        {
          value: 'danger',
          label: '危险'
        }
      ],
      // 获取用户列表数据的参数对象
      queryInfo: {
        // 当前的页数
        currentPage: 1,
        // 当前每页显示多少条数据
        pageSize: 10,
        columnName: undefined, //权限字段
        tableName:undefined, //表名
      },
      userList: [],
      total: 0,
      // 对话框显示与隐藏
      dialogVisible: false,
      // 添加用户的表单数据
      addForm: {
        columnName:undefined,
        remark: '',
        tableName: undefined,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
      },
      addFormRules:{
        columnName: [
          { required: true, message: '请输入权限字段', trigger: 'blur' },
        ],
        tableName: [
          { required: true, message: '请输入表名', trigger: 'blur' },
        ],
      }
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，el属性还不存在
  */
  created() {
    this.getList()
    this.handleSizeChange()
    this.handleCurrentChange()
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted() { },
  // 组件方法
  methods: {
     // 表名 下拉列表
     async getRoleList() {
      const { data: res } = await this.$http.get('/sys-table-scope/tables')
      if (res.code === 20000) {
        // console.log(this.queryParams);
        this.listOptions = res.data
      }
    },
     // 选中内容
   async  handleChange(value) {
      console.log(value);
      const { data: res } = await this.$http.get(`/sys-table-scope/columns?tableName=${value}`)
      if (res.code === 20000) {
        // console.log(this.queryParams);
        this.dataOptions = res.data
        console.log(res.data);
      }
    },
    // 字典数据列表
    async getList() {
      // this.getRoleList()
      // this.queryInfo.dictType = this.$route.query.dictType
      const params = this.queryInfo
      const { data: res } = await this.$http.post('/sys-table-scope/page', params)
      if (res.code === 20000) {
        this.userList = res.data.records
        console.log(res.data.records);
        this.addForm.dictCode = res.data.records.dictCode
        // console.log(res.data.records.dictCode);
        this.total = parseInt(res.data.total)
      } else {
        this.$message.error(res.msg)
      }
    },
    // 新增 弹窗
    addTypeHandle() {
      this.getRoleList()
      this.dialogVisible = true
      this.upload.title = "添加数据权限"
      console.log(1);
      this.handleChange()
    },
    // 修改按钮操作
    updateData(row) {
      this.getRoleList()
      this.handleChange(row.tableName)
      this.upload.title = "修改数据权限"
      this.dialogVisible = true
      this.addForm = row
    },
    // 删除 无弹窗
    // async deleteData(row) {
    //   const { data: res } = await this.$http.get(`/sys-table-scope/delete?scopeId=${[row]}`, )
    //   if (res.code === 20000) {
    //     this.getList()
    //     return this.$message.success('删除成功')
    //   } else {
    //     this.$message.error(res.msg)
    //   }
    // },
    // 删除弹窗封装
     async deleteData(row) {
      if (!(await this._box(row,'get','/sys-table-scope/delete','scopeId'))) return;
    },
    // 删除弹窗
    // deleteData(index) {
    //   console.log(index)
    //   this.$confirm('此操作将永久删除此数据, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(async () => {
    //     const { data: res } = await this.$http.get(`/sys-table-scope/delete?scopeId=${[index]}`, )
    //       console.log(res.code);
    //       if (res.code === 20000) {
    //         this.getList()
    //         this.$message.success('删除成功！')
    //       } else {
    //         this.$message.error('res.msg');
    //       }
    //   })
    // },
    // 提交按钮 添加修改类型
    addUser() {
      this.$refs['addFormRef'].validate(async valid => {
        if (valid) {
          if (this.addForm.scopeId != undefined) {
            console.log(this.addForm.scopeId, '修改');
            const params = this.addForm
            const { data: res } = await this.$http.post('/sys-table-scope/update', params)
            if (res.code === 20000) {
              this.$message.success(`修改成功!`)
              this.getList()
              this.dialogVisible = false
            } else {
              this.$message.error(res.msg)
            }
          } else {
            const params = this.addForm
            const { data: res } = await this.$http.post('/sys-table-scope/add', params)
            if (res.code === 20000) {
              this.$message.success(`新增成功!`)
              this.getList()
              this.dialogVisible = false
              this.$refs['addFormRef'].resetFields()
            } else {
              this.$message.error(res.msg)
            }
          }
        }
      })
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.currentPage = newPage
      this.getList()
    },
    // 关闭 返回到 定时任务页面
    closeViewHandler() {
      this.$router.push('/menu')
    },
    // 监听添加用户对话框关闭事件
    addDialogClosed() {
      // 重置表单
      this.$refs['addFormRef'].resetFields()
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryInfo.currentPage = 1
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      // this.resetForm('queryForm')
      // console.log(this) this指向$refs的是通过ref获得的实例对象loginFormRef
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
   
  }
}
</script>

<style scoped lang="less">
// 表格样式
.el-table {
  margin-top: 15px;
  font-size: 12px;
}
</style>
